<%--
  Created by IntelliJ IDEA.
  User: EDY
  Date: 2023-11-01
  Time: 8:55
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script src="../../js/jquery-3.3.1.js"></script>
    <script src="../../js/bootstrap-table.min.js"></script>
    <script src="../../js/bootstrap.bundle.min.js"></script>
    <script src="../../js/bootstrap-table-zh-CN.min.js"></script>
    <script src="../../js/jquery.form.js"></script>
    <script src="../../js/jquery.validate.js"></script>
    <script src="../../js/messages_zh.js"></script>
    <link rel="stylesheet" href="../../css/bootstrap-table.min.css"></link>
    <link rel="stylesheet" href="../../css/bootstrap.min.css"></link>
    <style>
        #form {
            position: absolute;
            top: 10%;
            bottom: 10%;
            left: 10%;
            right: 10%;
            margin: auto;
        }
    </style>
</head>
<body>
<table width="100%">
    <tbody>
    <tr height="20">
        <td>
            <span style="color: #0c84ff">您正在做的业务是：生产管理--工序物料设计--工序物料设计单变更</span>
        </td>
    </tr>
    <tr height="20">
        <td align="right">
            <input type="button" onclick="register()" value="提交">
            <input type="button" onclick="history.back()" value="返回">
        </td>
    </tr>
    </tbody>
</table>
<div>
    <table width="80%" align="center" border="0" bgcolor="#FFFFFF">
        <tbody>
        <tr>
            <td rowspan="2">
                <table cellspacing="0" cellpadding="0" width="100%" style="border: #333333 1px solid;">
                    <tbody>
                    <tr>
                        <td colspan="4" height="10">
                            <table width="100%">
                                <tbody>
                                <tr>
                                    <td>
                                        <table align="center">
                                            <tbody>
                                            <tr height="20">
                                                <td align="center" height="5"><font size="4"><b>工序物料设计单</b></font>
                                                </td>
                                            </tr>
                                            </tbody>
                                        </table>
                                        <table align="center">
                                            <tbody>
                                            <tr height="20">
                                                <td align="center" height="5">&nbsp;</td>
                                            </tr>
                                            </tbody>
                                        </table>
                                        <table align="center" id="theObjTable">
                                            <tbody>
                                            <tr height="20px">
                                                <td align="right" width="11%">
                                                    工序单编号：
                                                </td>
                                                <td width="30%">
                                                    <input type="text" readonly name="designId"
                                                           style="outline: none;border: 0px"></td>
                                                <td align="right" width="15%">
                                                    设计人：
                                                </td>
                                                <td width="30%">
                                                    <input type="text" readonly name="designer"
                                                           style="outline: none;border: 0px">
                                                </td>
                                                </td>
                                            </tr>
                                            <tr height="20px">
                                                <td align="right" width="10%">
                                                    产品名称：
                                                </td>
                                                <td width="35%">
                                                    <input type="text" name="productName"
                                                           style="outline: none;width: 100%;border:0px;">
                                                </td>
                                                <td align="right" width="10%">
                                                    产品编号：
                                                </td>
                                                <td width="35%">
                                                    <input type="text" name="productId"
                                                           style="outline: none;width: 100%;border:0px;">
                                                </td>
                                            </tr>
                                            </tbody>
                                        </table>
                                        <table align="center">
                                            <tbody>
                                            <tr height="20">
                                                <td align="center" height="5">&nbsp;</td>
                                            </tr>
                                            </tbody>
                                        </table>
                                        <table border="1" cellspacing="0" cellpadding="0"
                                               align="center" id="tableOnlineEdit" style="text-align: center;">
                                            <thead>
                                            <tr height="20" bgcolor="#D2E9FF">
                                                <td bordercolor="#DEDBD6" align="left" width="5%">
                                                    序号
                                                </td>
                                                <td bordercolor="#DEDBD6" align="left"
                                                    width="13%">工序名称
                                                </td>
                                                <td bordercolor="#DEDBD6" align="left"
                                                    width="11%">工序编号
                                                </td>
                                                <td bordercolor="#DEDBD6" align="left"
                                                    width="13%">描述
                                                </td>
                                                <td bordercolor="#DEDBD6" align="left" width="8%">
                                                    工时数(小时)
                                                </td>
                                                <td bordercolor="#DEDBD6" align="left"
                                                    width="11%">工时成本小计（元）
                                                </td>
                                                <td bordercolor="#DEDBD6" align="left"
                                                    width="13%">物料成本小计（元）
                                                </td>
                                                <td bordercolor="#DEDBD6" align="left"
                                                    width="13%">设计
                                                </td>
                                            </tr>
                                            </thead>
                                        </table>
                                        <table align="center">
                                            <tbody>
                                            <tr height="20">
                                                <td align="center" height="5">&nbsp;</td>
                                            </tr>
                                            </tbody>
                                        </table>
                                        <table align="center" id="theTable">
                                            <tbody>
                                            <tr height="20px">
                                                <td align="right" width="11%">
                                                    工时总成本：
                                                </td>
                                                <td width="30%">
                                                    <input type="text" readonly name="costPriceSum" style="outline: none;border: 0px"></td>
                                                <td align="right" width="15%">
                                                    物料总成本:
                                                </td>
                                                <td width="30%">
                                                    <input type="text" readonly name="moduleCostPriceSum" style="outline: none;border: 0px">
                                                </td>
                                            </tr>
                                            <tr height="20">
                                                <td align="right" width="9%">设计要求：
                                                </td>
                                                <td align="left" colspan="3" width="89%">
                                                    <input rows="4" name="moduleDescribe"
                                                           readonly style="border: 0px;outline: none"></input>
                                                </td>
                                            </tr>
                                            <tr height="20px">
                                                <td align="right" width="11%">
                                                    审核人：
                                                </td>
                                                <td width="30%">
                                                    <input type="text" readonly name="checker" style="outline: none;border: 0px"></td>
                                                <td align="right" width="15%">
                                                    审核时间:
                                                </td>
                                                <td width="30%">
                                                    <input type="text" readonly name="checkTime" style="outline: none;border: 0px">
                                                </td>
                                            </tr>
                                            </tbody>
                                        </table>
                                    </td>
                                </tr>
                                </tbody>
                            </table>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="3" nowrap="" width="56">
                        </td>
                    </tr>
                    <tr>
                        <td colspan="3" nowrap="" height="100"></td>
                        <td></td>
                    </tr>

                    <tr valign="bottom">
                        <td colspan="4" nowrap="" align="center"></td>
                    </tr>

                    <tr>
                        <td colspan="4" nowrap="" align="right"></td>
                    </tr>
                    </tbody>
                </table>
            </td>
            <td width="1" bgcolor="#ffffff" height="2"></td>
            <td width="1" bgcolor="#ffffff" height="2"></td>
            <td width="1" bgcolor="#ffffff" height="2"></td>
        </tr>
        <tr>
            <td width="1" bgcolor="#333333" height="120"></td>
            <td width="1" bgcolor="#666666" height="120"></td>
            <td width="1" bgcolor="#999999" height="120"></td>
        </tr>
        <tr valign="top" align="right">
            <td colspan="4">
                <table cellspacing="0" cellpadding="0" align="center" border="0">
                    <tbody>
                    <tr>
                        <td bgcolor="#333333" height="1"></td>
                    </tr>
                    <tr>
                        <td bgcolor="#666666" height="1"></td>
                    </tr>
                    <tr>
                        <td bgcolor="#999999" height="1"></td>
                    </tr>
                    </tbody>
                </table>
            </td>
        </tr>
        <tr valign="center" align="right" id="numzhs">
            <td colspan="4"></td>
        </tr>
        </tbody>
    </table>
</div>
<form class="modal fade" id="modalform" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content" style="width: 850px;height: 500px;position: absolute;left: -150px;">
            <h6><span style="color: #0c84ff">您正在做的业务是：生产管理--工序物料设计--制定工序物料设计单</span></h6>
            <div align="center"><h3>工序物料设计单</h3></div>
            <div>制造:<span name="productName"></span>产品，供选择的物料清单如下：</div>
            <div class="modal-header">
                <table class="table" id="DFileY0012Table" style="width: 690px">
                    <tr>
                        <td>
                            序号
                        </td>
                        <td>
                            物料名称
                        </td>
                        <td>
                            物料编号
                        </td>
                        <td>
                            设计数量
                        </td>
                        <td>
                            可用数量
                        </td>
                        <td>
                            单位
                        </td>
                        <td>
                            单价（元）
                        </td>
                        <td>
                            本工序数量
                        </td>
                    </tr>
                </table>
            </div>
            <input onclick="js()" type="button" value="提交">
        </div>
    </div>
</form>
<script>
    var i = 1;
    //var inpputcs=0;
    function showCostPrice(){
        var sum=0;
        $("#tableOnlineEdit :input[name='moduleSubtotal']").each(function (index,item) {
            sum=parseInt(sum)+parseInt($(item).val())
        })
        $("#theTable :input[name='moduleCostPriceSum']").val(sum)
    }
    $(function () {
        $.post("../../MDesignProcedure/showMDesignProcedureById", {id: '${param.id}'}, function (data) {
            $.each(data, function (key, value) {
                $("#theTable :input[name="+key+"]").val(value)
                $("#theObjTable :input[name="+key+"]").val(value)
            })
        })
        $.post("../../MDesignProcedure/showMDesignProcedureDetailsByParentId",{id:'${param.id}'},function (data) {
            $.each(data,function (index,item) {
                var tr=$("<tr>" +
                    "<td><input name='id' hidden style='outline: none;border: 0px;width:32px' value='"+item.id+"'><input style='outline: none;border: 0px;width:32px' value='"+i+"'></td>" +
                    "<td><input name='procedureName' readonly style='outline: none;width:140px;border: 0px' value='" + item.procedureName + "'></td>" +
                    "<td><input name='procedureId' readonly style='outline: none;width:120px;border: 0px' value='" + item.procedureId + "'></td>" +
                    "<td><input name='procedureDescribe' readonly style='outline: none;width:120px;border: 0px' value='" + item.procedureDescribe + "'></td>" +
                    "<td><input name='labourHourAmount' value='"+item.labourHourAmount+"' readonly style='border: 0px;width: 100px;outline: none'></td>" +
                    "<td><input name='subtotal' value='"+item.subtotal+"' readonly style='outline: none;width:144px;border: 0px;'></td>" +
                    "<td><input name='moduleSubtotal' value='"+(item.moduleSubtotal==null?'0.00':item.moduleSubtotal)+"' readonly style='outline: none;width:144px;border: 0px;'></td>" +
                    "<td><div style='width: 64px'>"+
                    "<a href='javascript:void(0)' onclick='dk(this)'>变更</a>"+
                    "</div></td>" +
                    "</tr>")
                i=i+1;
                $("#tableOnlineEdit").append(tr);
            })
            showCostPrice()
        })
    })
    $("#add").click(function () {
        $("#modalform").modal("show");
    })
    var p=1;
    var id=0;
    function dk(a) {
        if(confirm("你确认要重新设计吗？重新设计将会清空当前工序所使用的物料")){
            var productId=$("#theObjTable :input[name='productId']").val()
            id=$(a).parent().parent().parent().find("[name='id']").val();
            $.post("../../MDesignProcedure/removeById",{productId:productId,id:id},function (data) {
                var productName=$("#theObjTable :input[name=productName]").val()
                $("#modalform").modal("show")
                $("#modalform [name=productName]").text(productName)
                $("#DFileY0012Table tr:not(:first)").remove()
                p=1;
                $.post("../../MDesignProcedure/showProuctByProductId", {productId:productId}, function (data) {
                    $.each(data, function (index, item) {
                        var tr=$("<tr>" +
                            "<td><input name='detailsNumber' readonly style='width:32px;outline: none; border: 0px;' value='"+item.detailsNumber+"'></td>" +
                            "<td>" +
                            "<input name='parentId' value='"+id+"' hidden style='width: 105px;outline:none;border:0px;'>" +
                            "<input name='productName' value='"+item.productName+"' readonly style='width: 105px;outline:none;border:0px;'></td>" +
                            "<td><input name='productId' value='"+item.productId+"' readonly style='width: 140px;outline:none;border:0px;'></td>"+
                            "<td>" +
                            "<input value='"+item.amount+"' readonly style='width: 64px;outline:none;border:0px;'>"+
                            "<input name='type' hidden value='"+item.type+"' readonly style='width: 64px;outline:none;border:0px;'>" +
                            "</td>"+
                            "<td>" +
                            "<input value='"+item.residualAmount+"' readonly style='width: 64px;outline:none;border:0px;'>" +
                            "<input hidden name='productDescribe' value='"+item.productDescribe+"' readonly style='width: 64px;outline:none;border:0px;'>" +
                            "</td>"+
                            "<td><input name='amountUnit' value='"+item.amountUnit+"' readonly style='width: 32px;outline:none;border:0px;'></td>"+
                            "<td><input name='costPrice' value='"+item.costPrice+"' readonly style='width: 85px;outline:none;border:0px;'></td>"+
                            "<td><input name='amount' type='number' min='0' style='width: 105px;outline:none;border:0px;background: bisque'></td>"+
                            "</tr>")
                        p=p+1;
                        $("#DFileY0012Table").append(tr)
                    })
                    showCostPrice()
                })
            })
        }
    }
    var sn=0;
    function js() {
        $("#DFileY0012Table :input[name='amount']").each(function (index,item) {
            if(sn==1){
                return
            }
            if($(item).val()==""){
                sn=1;
                alert("请填写完数量后在进行提交操作!")
                $(this).select()
                return;
            }
            var sum=$(item).parent().prev().prev().prev().children().val()
            var num=$(item).val()
            if(sum<num){
                sn=1;
                alert("数量不可大于可用数量")
                $(this).select()
                return;
            }
        })
        if(sn==1){
            sn=0;
            return
        }var arr = new Array();
        var productId=$("#theObjTable :input[name='productId']").val()
        $("#DFileY0012Table :input[name='detailsNumber']").each(function(){
            var parentId=id;
            var detailsNumber=$(this).val()
            var productId=$(this).parent().parent().find("[name='productId']").val();
            var productName=$(this).parent().parent().find("[name='productName']").val();
            var type=$(this).parent().parent().find("[name='type']").val()
            var amount=$(this).parent().parent().find("[name='amount']").val()
            var productDescribe=$(this).parent().parent().find("[name='productDescribe']").val()
            var amountUnit=$(this).parent().parent().find("[name='amountUnit']").val()
            var costPrice=$(this).parent().parent().find("[name='costPrice']").val()
            var subtotal=parseInt(costPrice)*parseInt(amount)
            var a={
                parentId:parentId,
                detailsNumber:detailsNumber,
                productId:productId,
                productName:productName,
                type:type,
                amount:amount,
                productDescribe:productDescribe,
                amountUnit:amountUnit,
                costPrice:costPrice,
                subtotal:subtotal
            }
            arr.push(a)
        });
        var arr2={productId:productId,list:arr}
        $.ajax({
            url:"/MDesignProcedure/mDesignProcedureModuleRegister",
            type:"post",
            data:JSON.stringify(arr2),
            contentType:'application/json',
            dataType:"json",
            success:function (data) {
                alert(data.msg)
                if(data.code=='1'){
                    location.href="update.jsp?id=${param.id}";
                }
            }
        })
    }
    var sn=0;
    function register() {
        var productId=$("#theObjTable :input[name='productId']").val()
        $.post("/MDesignProcedure/showProuctByProductId",{productId:productId},function (data) {
            $.each(data, function (index, item) {
                if(sn==1){
                    return
                }
                if(item.residualAmount!=0){
                    alert("请将产品物料全部用完!")
                    sn=1;
                    return;
                }
            })
            if(sn==1){
                sn=0;
                return
            }
            var moduleCostPriceSum=$("#theTable :input[name='moduleCostPriceSum']").val()
            var arr={id:'${param.id}',moduleCostPriceSum:moduleCostPriceSum}
            $.ajax({
                url:"/MDesignProcedure/updMDesignProcedure",
                type:"post",
                data:JSON.stringify(arr),
                contentType:'application/json',
                dataType:"json",
                success:function (data) {
                    alert(data.msg)
                    if(data.code=='1'){
                        location.href="register_list.jsp";
                    }
                }
            })
        })
    }
</script>
</form>
</body>
</html>
